<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    //ThinkPHP的构造函数
    public function _initialize() {
        //自动加载的类
        //new \Org\Autoload();
        //根据配置来开启Redis 1为开启Redis缓存 2为文件缓存用于本地测试
        if(C('REDIS_OPEN')==1){
            //300秒 5分钟数据缓存
            S(array('type'=>'redis','host'=>'127.0.0.1','port'=>'6379','prefix'=>'index','expire'=>300));
        }else{
            S(array('type'=>'file','prefix'=>'index','expire'=>5));
        }
    }

    public function index(){
        $setup=M('Setup')->field('value')->select();
        $meta=$setup[0]['value'];
        $this->assign('meta',$meta);
        $Model = D('user_book');
        $company_list = $Model->query('select distinct department1 from think_user_book');
        $this->assign('company_list',$company_list);

        $start_date = $setup[1]['value'];
        //echo $start_date.'<br/>';
        $end_date = $setup[2]['value'];
        //echo $end_date;exit;
        $now_date = date('Y-m-d H:i:s');

        $date_states = 'can_sign';//not_start:未开始；can_sign:可以投票；game_over:已经结束
        if(strtotime($now_date)<strtotime($start_date)){
            $date_states = 'not_start';
        }else if(strtotime($now_date)>strtotime($end_date)){
            $date_states = 'game_over';
        }else{
            if(session('member_id')){
                header('Location:/home/index/articleVote3');exit;
            }
        }
        //echo $date_states;exit;
        $this->assign('date_state',$date_states);
        $this->assign('start_date',$start_date);
        $this->display();
    }

    //签到check mobile---->mobile+idCard
    public function checklogin(){
        if(!empty($_POST)) {
            //$dept1 = $_POST['dept1'];
            //$dept2 = $_POST['dept2'];
            $name =addslashes($_POST['name']);
            $identity =addslashes($_POST['identity']);

            $staffModel = D("user_book");
            $list = $staffModel->where("right(identity,4)='".$identity."' and name='".$name."' and type=1")->select();//姓名+身份证号后4位
            if(count($list)>0){
                $_SESSION['member_id']     =   $list[0]['id'];
                session('member_id',$list[0]['id']);
                echo "success";exit;//alert('签到成功');
            }else{
                echo "fail";exit;//alert('抱歉,您未加入活动邀请名单,无法继续参与投票活动');
            }
        }else{
            echo '发现错误';exit;
        }
    }

    public function articleVote(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $member_id=$_SESSION['member_id'];

        $articleModel = D("article");
        //$article_list = $articleModel->where('article_type=1')->order('article_sort asc,article_id desc')->select();
        $article_list=$articleModel->query("select a.article_id,a.article_title,a.article_thumb,a.article_image,a.article_type,a.article_add_time,a.article_show,b.category_id,b.category_name from `think_article` a left join `think_article_category` b on a.category_id=b.category_id where a.article_type=1 and a.article_show=1 order by b.category_sort asc,a.article_type asc,a.article_sort asc,a.article_id asc");

        $voteModel = D("vote");
        $vote_list = $voteModel->where('member_id='.$member_id)->select();//我的投票记录

        if (!empty($article_list) && is_array($article_list)){
            foreach ($article_list as $k=>$v){
                $is_vote = false;
                foreach ($vote_list as $a=>$b){
                    if($v['article_id']==$b['article_id']){
                        $is_vote = true;
                        break;
                    }
                }
                $v['is_vote'] = $is_vote;

                $article2_list = $articleModel->where('article_type=2 and category_id='.$v['category_id'])->select();
                $v['article2_count'] = count($article2_list);

                foreach ($article2_list as $d=>$f){
                    $is_vote = false;
                    foreach ($vote_list as $a=>$b){
                        if($f['article_id']==$b['article_id']){
                            $is_vote = true;
                            break;
                        }
                    }
                    $f['is_vote'] = $is_vote;
                    $article2_list[$d]	= $f;
                }

                $v['article_actor'] = $article2_list;

                $article_list[$k]	= $v;
            }
        }

        $this->assign('article_list',$article_list);
        $this->display();
    }

    public function articleVote3(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $member_id=$_SESSION['member_id'];

        $articleModel = D("article");
        //$article_list = $articleModel->where('article_type=1')->order('article_sort asc,article_id desc')->select();
        $article_list=$articleModel->query("select a.article_id,a.article_title,a.article_thumb,a.article_image,a.article_type,a.article_add_time,a.article_show,b.category_id,b.category_name from `think_article` a left join `think_article_category` b on a.category_id=b.category_id where a.article_type=1 and a.article_show=1 order by b.category_sort asc,a.article_type asc,a.article_sort asc,a.article_id asc");

        $voteModel = D("vote");
        $vote_list = $voteModel->where('member_id='.$member_id)->select();//我的投票记录

        if (!empty($article_list) && is_array($article_list)){
            foreach ($article_list as $k=>$v){
                $is_vote = false;
                foreach ($vote_list as $a=>$b){
                    if($v['article_id']==$b['article_id']){
                        $is_vote = true;
                        break;
                    }
                }
                $v['is_vote'] = $is_vote;

                $article2_list = $articleModel->where('article_type=2 and category_id='.$v['category_id'])->select();
                $v['article2_count'] = count($article2_list);

                $article_list[$k]	= $v;
            }
        }

        $this->assign('article_list',$article_list);
        $this->display();
    }

    //查看所有节目列表
    public function articleList(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $articleModel = D("article");
        $article_list = $articleModel->where('article_type=1')->order('article_sort asc,article_id desc')->select();

        if (!empty($article_list) && is_array($article_list)){
            foreach ($article_list as $k=>$v){
                $article2_list = $articleModel->where('article_type=2 and category_id='.$v['category_id'])->select();
                $v['article2_count'] = count($article2_list);

                $article_list[$k]	= $v;
            }
        }

        $this->assign('article_list',$article_list);
        $this->display();
    }

    //根据节目查看节目的所有演员
    public function articleDetail(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $category_id = $_GET['category_id'];
        $member_id=$_SESSION['member_id'];

        $voteModel = D("vote");
        $vote_list = $voteModel->where('member_id='.$member_id)->select();//我的投票记录

        $articleModel = D("article");
        $article_list = $articleModel->where('category_id='.$category_id)->order('article_sort asc,article_id desc')->select();

        foreach ($article_list as $k=>$v){
            $is_vote = false;
            foreach ($vote_list as $a=>$b){
                if($v['article_id']==$b['article_id']){
                    $is_vote = true;
                    break;
                }
            }

            if($v['article_type']==1){//1节目 2演员
                $article_info = $v;
                $article_info['is_vote'] = $is_vote;
            }else if($v['article_type']==2){
                $article_type2_list[$k]	= $v;
                $article_type2_list[$k]['is_vote']	= $is_vote;
            }
        }

        $this->assign('article_type2_list',$article_type2_list);
        $this->assign('article_info',$article_info);

        $this->display();
    }

    //查看自己的投票结果
    public function vote_result(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $articleModel = D("article");
        $list = $articleModel->where('article_id in (select article_id from think_vote where article_type=1 and member_id='.$_SESSION['member_id'].')')->order('article_type asc,article_sort asc,article_id desc')->select();
        $this->assign('count_result',count($list));

        foreach ($list as $k=>$v){
            if($v['article_type']==1) {//1节目 2演员
                $article2_list = $articleModel->where('article_type=2 and category_id='.$v['category_id'])->select();
                $v['article2_count'] = count($article2_list);

                $article_list[$k]	= $v;
            }
        }
        $this->assign('article_list',$article_list);
        $this->display();
    }

    //投票
    public function vote(){
        $article_id = $_POST['article_id'];
        $article_type = $_POST['article_type'];
        $member_id=$_SESSION['member_id'];

        $voteModel = D("vote");

        $vote_list = $voteModel->where('member_id='.$member_id.' and article_type='.$article_type)->select();

        if(count($vote_list)>=3){echo 'more3_error'; exit;}
        if (!empty($vote_list) && is_array($vote_list)) {
            foreach ($vote_list as $k => $v) {
                if($v['article_id']==$article_id){
                    echo 'repeat_error' ;exit;
                }
            }
        }

        $arr['member_id'] = $member_id;
        $arr['article_id'] = $article_id;
        $arr['vote_time'] = time();
        $arr['article_type'] = $article_type;
        $insert_id = $voteModel->add($arr);

        if($insert_id){//success
            //更新article表投票数
            M('Article')->where('article_id='.$article_id)->setInc('article_vote');
            echo 1; exit;
        }else{
            echo 0; exit;
        }
    }

    //撤票
    public function cancelVote(){
        $article_id=addslashes($_POST['article_id']);
        $member_id=$_SESSION['member_id'];
        $voteModel = D("vote");
        $condition['member_id']=$member_id;
        if(!empty($_POST['article_id'])){
            $condition['article_id']=$article_id;
        }
        $voteModel->where($condition)->delete();//get boolean
        //更新article表投票数
        M('Article')->where('article_id='.$article_id)->setDec('article_vote');
        echo 1; exit;
    }

    //一键撤票
    public function cancelAllVote(){
        //echo 1;exit;
        //$article_id=addslashes($_POST['article_id']);
        $member_id=$_SESSION['member_id'];
        $voteModel = D("vote");
        $condition['member_id']=$member_id;
        //取得该用户所投文章id列表
        $article_id_list=M('Vote')->where($condition)->field('article_id')->select();
        $result=$voteModel->where($condition)->delete();//get boolean
        if($result===false){
            echo 2; exit;
        }else{
            //更新article表投票数
            foreach($article_id_list as $k=>$v){
                //echo $v['article_id'];exit;
                $result=M('Article')->where('article_id='.$v['article_id'])->setDec('article_vote');
                if($result===false){
                    echo 2;exit;
                }
            }
            echo 1;exit;
        }
    }

    //查看所有投票汇总
    public function vote_list(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $article_type = $_GET['article_type'];
        if(empty($article_type)){
            $article_type = 1;
        }

        $Model=D("Article");
        $article_list = $Model->get_allvote_list($article_type);//节目排名
        //$article_type2_list = $Model->get_allvote_list(2);//演员排名

        $this->assign('article_list',$article_list);
       // $this->assign('article_type2_list',$article_type2_list);

        $this->display();
    }

    public function vote_article(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $Model=D("Article");
        $article_list = $Model->get_allvote_list(1);//节目排名

        $this->assign('article_list',$article_list);
        $this->display();
    }

    public function vote_actor(){
        //未登录则跳转
        if(!session('member_id')){
            header('Location:/');exit;
        }
        $Model=D("Article");
        $article_list = $Model->get_allvote_list(2);//演员排名

        $this->assign('article_list',$article_list);
        $this->display();
    }

    public function getGroupbyCom(){
        $company = $_POST['company'];

        $Model = D('user_book');
        $group_list = $Model->query('select distinct department2 from think_user_book where department1=\''.$company.'\'');

        $this->ajaxReturn($group_list);
    }

    //查看所有投票汇总
    public function result(){
        $Model=D("Article");
        $article_list = $Model->get_allvote(1);//节目排名

        $this->assign('article_list',$article_list);
        $this->display();
    }
}